package com.gams.springboot.services.impl;

import com.gams.springboot.dao.RoleDao;
import com.gams.springboot.dao.UserDao;
import com.gams.springboot.pojo.Role;
import com.gams.springboot.pojo.User;
import com.gams.springboot.services.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


/**
 * @author tql
 * @current springboot
 * @create 2021-09-2-11:13
 */
@Service
@EnableAutoConfiguration
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userdao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<User> getUserList() {
        return userdao.getUserList();
    }

    @Override
    @Transactional
    public boolean addUser(User user) {
        if(userdao.addUser(user)>0){
            return true;
        }
        return false;
    }

    @Override
    public int deleteUser(long id) {
        return userdao.deleteUser(id);
    }

    @Override
    public int selectUser(String name, String password) {
        if(userdao.selectUser(name,password)>0){
            return 1;
        }
        return 0;
    }

    @Override
    public User selectUserByName(String name) {
        return userdao.selectUserByName(name);
    }


    @Override
    public PageInfo findAllUser(int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        //获取用户列表
        List<User> user = userdao.getUserList();
        //加载到分页信息类里面
        PageInfo<User>  pageInfo = new PageInfo(user);
        return pageInfo;
    }

//    @Override
//    public PageInfo searchAllUser(User user, int page, int limit) {
//        PageHelper.startPage(page,limit);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("name",user.getName());
//        map.put("account",user.getAccount());
//        map.put("status",user.getStatus());
//        //获取用户列表
//        List<User> user1 = userdao.searchUser(map);
//        //加载到分页信息类里面
//        PageInfo<User>  pageInfo = new PageInfo(user1);
//        return pageInfo;
//    }

    @Override
    public int updateUser(User user) {
        return userdao.updateUser(user);
    }

    @Override
    public int insertUserRole(long userId, long roleId) {
        return userdao.insertUserRole(userId,roleId);
    }

    @Override
    public int queryId(String name) {

        return userdao.queryId(name);
    }

    @Override
    public List<Long> selectRoleIdByUserId(long userId) {
        List<Long> roleIdList = userdao.selectRoleIdByUserId(userId);
        return roleIdList;
    }

    @Override
    public int deleteUserRoleById(long id) {
        return userdao.deleteUserRoleById(id);
    }

    @Override
    public int updateUserPassword(String password, long id) {
        return userdao.updateUserPassword(password,id);
    }

    @Override
    public List<User> searchUser(String name,String account,String status) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("account",account);
        map.put("status",status);
        return userdao.searchUser(map);
    }
}
